﻿namespace ThreeByte.ActiveDeck.Dashboard
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using System;
    using ActiveDeck.Dashboard.LoginUI;
    using ThreeByte.ActiveDeck.Web.Models;
    using ThreeByte.ActiveDeck.Dashboard.Controls;
    using System.Windows;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home() {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;
        }

        private static readonly string DEVELOPER_ROLE = @"Developer";

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e) {
            DeveloperIndicatorText.Visibility = (WebContext.Current.User.IsInRole(DEVELOPER_ROLE) ? Visibility.Visible : Visibility.Collapsed);
        }

        private void DashboardDataSource_LoadedData(object sender, LoadedDataEventArgs e) {

        }

        private void DeletePresentationButton_Click(object sender, System.Windows.RoutedEventArgs e) {
            //Delete the requested presentation
            Button deleteButton = (Button)sender;

            Presentation selectedPresentation = (Presentation)(deleteButton.Tag);
            //Console.WriteLine("Presentation: " + selectedPresentation);

            //Confirm Delete
            ConfirmDeleteDialog confirmDialog = new ConfirmDeleteDialog(selectedPresentation);
            confirmDialog.Closed += new EventHandler(confirmDialog_Closed);
            confirmDialog.Show();        

        }

        void confirmDialog_Closed(object sender, EventArgs e) {
            ConfirmDeleteDialog confirmDialog = (ConfirmDeleteDialog)sender;
            confirmDialog.Closed -= confirmDialog_Closed;

            if(confirmDialog.DialogResult ?? false) {
                DashboardDataSource.DataView.Remove(confirmDialog.Presentation);
                //Submit changes
                DashboardDataSource.SubmitChanges();
            }

            Console.WriteLine("Presentation removed");
        }

        private void InspectPresentationButton_Click(object sender, RoutedEventArgs e) {
            //Delete the requested presentation
            Button button = (Button)sender;
            Presentation selectedPresentation = (Presentation)(button.Tag);

            //Navigate to the appropriate inspect page
            NavigationService svc = NavigationService;
            if(svc == null){
                return;
            }
            svc.Navigate(new Uri(string.Format("/Inspect/{0}", selectedPresentation.ID), UriKind.Relative));
        }
    }
}